package a66weding.com.jiehuntong.activity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import a66weding.com.jiehuntong.MainActivity;
import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.beans.VerifiCodeLoginBean;
import a66weding.com.jiehuntong.common.MyActivityManager;
import a66weding.com.jiehuntong.common.MyMeActivityManager;
import a66weding.com.jiehuntong.common.WebAddress;
import a66weding.com.jiehuntong.common.api.Url;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.CacheUtils;
import a66weding.com.jiehuntong.common.utils.MD5Util;
import a66weding.com.jiehuntong.common.utils.Tools;
import a66weding.com.jiehuntong.common.utils.Utils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.NetStatusUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.scanner.CaptureActivity;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

import static android.app.Activity.RESULT_OK;

/**
 * 老版本密码登录与短信登录共存版
 */
public class LoginActivity extends CommonActivity implements View.OnClickListener, OnNetResultListener {
    private Button mLogBtn;
    private TextView mToRegView;
    private TextView mToMainActivity;
    private EditText mPhoneNumEt;// 手机号
    private EditText mVerifiCodeEt;// 验证码
    private TextView mGetVerifiCodeTv;// 获取验证码
    private TextView mResetPsdTv;
    private LinearLayout mBackLay;
    private String phoneNumber;
    private boolean mExitFlag;
    private static final int WHAT_EXIT_FLAG = 0x128;
    private User userBean;
    private boolean isCanClickable = true;
    private static final int LOGINBTN = 1;
    private final static int SCANNIN_GREQUEST_CODE = 12;
    private static final int PERMISSION_CAMERA = 5;
    private final int RESETTEXT = 11;//点击获取验证码后就发送后倒计时
    private final int GETVERIFIFAIL = 12;//获取验证码失败，需要重置
    private static int time = 60;
    private boolean isCanBack;//是否返回登录的上一个界面
    boolean isSended = false;
    public static int LOGIN_OK = 200;
    private TextView mTvVerificationOrPWD;
    private EditText mEtPassWordLogin;
    private boolean isPWD = true;//是否是密码登录，这里分为密码和验证码
    private RelativeLayout mVerificationRlSms;
    private RelativeLayout mActivityRoot;
    private final int GOREGIST = 100;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOGINBTN:
                    isCanClickable = true;
                    break;
                case WHAT_EXIT_FLAG:
                    if (null != LoginActivity.this) {
                        LoginActivity.this.mExitFlag = false;
                    }
                    break;
                case GETVERIFIFAIL://获取验证码失败
                    mHandler.removeMessages(RESETTEXT);
                    mGetVerifiCodeTv.setText(R.string.huoquyanzhen);
                    mGetVerifiCodeTv.setClickable(true);
                    mGetVerifiCodeTv.setPressed(false);//红色字体
                    time = 60;
                    break;
                case RESETTEXT:
                    if (time <= -1) {
                        mHandler.sendEmptyMessage(GETVERIFIFAIL);
                    } else {
                        mGetVerifiCodeTv.setText("" + time--);
                        mHandler.sendEmptyMessageDelayed(RESETTEXT, 1000);
                        mGetVerifiCodeTv.setPressed(true);//棕灰色字体
                    }
                    break;

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShareSDK.initSDK(this);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("LoginActivity", this);
        boolean mIslogin = ((GlobalApplication) getApplication()).isLogin();
        Intent intent = getIntent();
        if (intent != null) {
            isCanBack = intent.getBooleanExtra("isCanBack", true);
        }
        User user = DataWareHourse.getInstance().getmUser();
        int i_id = 0;
        if (user != null) {
            i_id = user.getData().get(0).getI_Id();
        } else {
            try {
                user = CacheUtils.readObjectFromTextFile(RequestCode.TOLOGIN_ACTIVITY_REQUESTCODE, User.class);
                if (user != null) {
                    i_id = user.getData().get(0).getI_Id();
                    saveUserInfo(user);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (mIslogin && user != null && i_id != 0) {
            setLoginFinishResult(true);
        } else {
            ((GlobalApplication) getApplication()).setLogin(false);
            CacheUtils.setBooleanToCache(this, CacheUtils.islogin, false);
        }
        initView();
    }

    //QQ登录
    public void imgBtnQQLogin(View view) {
        Platform weibo = ShareSDK.getPlatform(QQ.NAME);
        //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执行
        weibo.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                arg2.printStackTrace();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                //输出所有授权信息
                String s = arg0.getDb().exportData();
                Log.i("QQ", "onComplete: " + s);
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {

            }
        });
        //authorize与showUser单独调用一个即可
        // weibo.authorize();//单独授权,OnComplete返回的hashmap是空的
        weibo.showUser(null);//授权并获取用户信息
        //移除授权
        //weibo.removeAccount(true);
    }

    //微信登录
    public void imgBtnWeChatLogin(View view) {
        Platform weibo = ShareSDK.getPlatform(Wechat.NAME);
        //回调信息，可以在这里获取基本的授权返回的信息，但是注意如果做提示和UI操作要传到主线程handler里去执 行
        weibo.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                arg2.printStackTrace();
            }

            @Override
            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                //输出所有授权信息
                String s = arg0.getDb().exportData();
                Log.i("WeiXin", "onComplete: " + s);
            }

            @Override
            public void onCancel(Platform arg0, int arg1) {

            }
        });
        //authorize与showUser单独调用一个即可
        // weibo.authorize();//单独授权,OnComplete返回的hashmap是空的
        weibo.showUser(null);//授权并获取用户信息
        //移除授权
        //weibo.removeAccount(true);
    }

    private void initView() {
        mActivityRoot = (RelativeLayout) findViewById(R.id.activityRoot);
        mGetVerifiCodeTv = (TextView) findViewById(R.id.tv_register_getverficode_sms);
        mGetVerifiCodeTv.setOnClickListener(this);
        mVerifiCodeEt = (EditText) findViewById(R.id.et_verification);
        mVerificationRlSms = (RelativeLayout) findViewById(R.id.verification_rl_sms);
        mTvVerificationOrPWD = (TextView) findViewById(R.id.tv_verification_or_pwd);
        mTvVerificationOrPWD.setOnClickListener(this);
        mEtPassWordLogin = (EditText) findViewById(R.id.et_password_login);
        mToRegView = (TextView) findViewById(R.id.tv_register);
        mLogBtn = (Button) findViewById(R.id.btn_login);
        mToMainActivity = (TextView) findViewById(R.id.tv_nowantlog);
        mPhoneNumEt = (EditText) findViewById(R.id.et_login_phone_number);
        mPhoneNumEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mHandler.sendEmptyMessage(GETVERIFIFAIL);//删除手机号后快速再次发送
           /*     if (isSended) {
                    time = 0;
                }*/
            }
        });
        mResetPsdTv = (TextView) findViewById(R.id.tv_resetpsd);
        mBackLay = (LinearLayout) findViewById(R.id.lay_back);
        mBackLay.setVisibility(View.INVISIBLE);
        if (isCanBack) {
            mBackLay.setVisibility(View.VISIBLE);
            mBackLay.setOnClickListener(this);
        }
        mLogBtn.setOnClickListener(this);
        mToRegView.setOnClickListener(this);
        mToMainActivity.setOnClickListener(this);
        mResetPsdTv.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_register_getverficode_sms:// 获取短信验证码
                String phoneNumbers = mPhoneNumEt.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNumbers)) {
                    CUtils.showToast("手机号码不能为空！");
                    return;
                }
                if (!Utils.isPhone(phoneNumbers)) {
                    CUtils.showToast("手机号码格式有误！");
                    return;
                }
                if (NetStatusUtil.isNetworkAvailable(this)) {
                    mGetVerifiCodeTv.setClickable(false);
                    time = 60;
                    mHandler.sendEmptyMessage(RESETTEXT);
                    getVerifiCode();
                } else {
                    CUtils.showToast("网络连接异常，请确认网络正常！");
                }
                break;
            case R.id.tv_verification_or_pwd://登录方式切换按钮
            /*    if (isPWD) {
                    mEtPassWordLogin.setVisibility(View.GONE);
                    mVerificationRlSms.setVisibility(View.VISIBLE);
                    mTvVerificationOrPWD.setText(R.string.pwdlogin);
                    isPWD = false;
                } else {
                    mEtPassWordLogin.setVisibility(View.VISIBLE);
                    mVerificationRlSms.setVisibility(View.GONE);
                    mTvVerificationOrPWD.setText(R.string.phonechecklogin);
                    isPWD = true;
                }*/
              Intent intent = new Intent(this, LoginForSMSActivity.class);
                startActivityForResult(intent,600);
                break;
            case R.id.btn_login://登录按钮
                if (isCanClickable) {
                    if (isPWD) {
                        toLogin();
                        isCanClickable = false;
                        mHandler.sendEmptyMessageDelayed(LOGINBTN, 2000);
                    } else {
                        toLogin();
                        isCanClickable = false;
                        mHandler.sendEmptyMessageDelayed(LOGINBTN, 2000);
                        /*   if (isSended) {
                     }else {
                              CUtils.showToast("请先获取验证码！");
                        }*/
                    }
                }
                break;
            case R.id.tv_register:
                //注册商家或者普通客户弹窗
                //showDialog();暂时取消商家注册，直接普通用户注册
                startRegActivity();
                break;
            case R.id.tv_nowantlog://随便逛逛
                if (isCanBack) {
                    finish();
                } else {
                    ((GlobalApplication) getApplication()).isLogin = false;
                    isCanBack = false;//随便逛逛可以在返回到这个页面，并且不会返回登录结果
                    setLoginFinishResult(false);
                }
                break;
            case R.id.tv_resetpsd://忘记密码（重置密码）
                toRestPsdActivity();
                break;
            case R.id.lay_back://返回按钮
                finish();
                break;
            case R.id.tv_register_getverficode:// 获取短信验证码
                String phoneNumber = mPhoneNumEt.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNumber)) {
                    CUtils.showToast("手机号码不能为空！");
                    return;
                }
                if (!Utils.isPhone(phoneNumber)) {
                    CUtils.showToast("手机号码格式有误！");
                    return;
                }
                boolean networkAvailable = NetStatusUtil.isNetworkAvailable(this);
                if (networkAvailable) {
                    mGetVerifiCodeTv.setClickable(false);
                    mHandler.sendEmptyMessage(RESETTEXT);
                    getVerifiCode();
                } else {
                    CUtils.showToast("网络连接异常，请确认网络正常！");
                }
                break;
        }

    }

    /**
     * 显示注册对话框;
     */
    public void showDialog() {
        View view = View.inflate(this, R.layout.registrationdialog, null);
        //点击商家注册
        view.findViewById(R.id.rigester_sj).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    //申请CAMERA权限
                    ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CAMERA);
                } else {
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this, CaptureActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
                }
                Tools.dismissWaitDialog();

            }
        });
        //点击普通注册;
        view.findViewById(R.id.rigester_pt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startRegActivity();
                Tools.dismissWaitDialog();
            }
        });
        Tools.showRegisterDialog(view, this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        doNext(requestCode, grantResults);
    }

    private void doNext(int requestCode, int[] grantResults) {
        if (requestCode == PERMISSION_CAMERA) {
            if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, CaptureActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
            } else {
                CUtils.showToast("请打开手机拍照权限后在操作");
            }
        }
    }

    /**
     * 获取验证码
     */
    public void getVerifiCode() {
        //请求参数
        String url = Url.VERFICATIONCODE + MD5Util.getMD5String(mPhoneNumEt.getText().toString().trim() + "00dl")
                + "&Mobile=" + mPhoneNumEt.getText().toString().trim() + "&type=dl";
        HttpRequestBean requestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, LoginActivity.this, VerifiCodeLoginBean.class, Url.VERFICATIONCODE);
        HttpHandleUtil.executeCommonPost(LoginActivity.this, requestBean);
    }

    /**
     * 校验并登陆
     */
    public void toLogin() {
        if (isPWD) {
            phoneNumber = mPhoneNumEt.getText().toString().trim();
            if (TextUtils.isEmpty(phoneNumber)) {
                CUtils.showToast("手机号码不能为空！");
                return;
            }
            if (!Utils.isPhone(phoneNumber)) {
                CUtils.showToast("手机号码格式有误！");
                return;
            }
            String mPassword = mEtPassWordLogin.getText().toString().trim();
            if (TextUtils.isEmpty(mPassword)) {
                CUtils.showToast("密码不能为空！");
                return;
            }
            if (mPassword.length() < 6) {
                CUtils.showToast("密码必须为6位以上！");
                return;
            }
            if (mPassword.contains(" ")) {
                CUtils.showToast("密码不能包含空格！");
                return;
            }

        } else {
            phoneNumber = mPhoneNumEt.getText().toString().trim();
            if (TextUtils.isEmpty(phoneNumber)) {
                CUtils.showToast("手机号码不能为空！");
                return;
            }
            if (!Utils.isPhone(phoneNumber)) {
                CUtils.showToast("手机号码格式有误！");
                return;
            }
            String verifyCode = mVerifiCodeEt.getText().toString().trim();
            if (verifyCode != null && TextUtils.isEmpty(verifyCode)) {
                CUtils.showToast("验证码不能为空！");
                return;
            }
        }
        requseNetLogin();
    }


    /**
     * 网络请求密码并登陆
     */
    private void requseNetLogin() {
        boolean networkAvailable = NetStatusUtil.isNetworkAvailable(this);
        if (networkAvailable) {
            if (isPWD) {
                String url = Url.PASSWORDLOGIN + "&nvc_MobilePhone=" + mPhoneNumEt.getText().toString().trim() + "&nvc_PassWord=" + mEtPassWordLogin.getText().toString().trim();
                HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, LoginActivity.this, User.class, "PasswordLogin");
                HttpHandleUtil.executeCommonPost(LoginActivity.this, httpRequestBean);
            } else {

                String url = Url.MEMBERLOGIN + MD5Util.getMD5String(mPhoneNumEt.getText().toString().trim() + "00" + mVerifiCodeEt.getText().toString().trim()) + "&nvc_MobilePhone=" + mPhoneNumEt.getText().toString().trim() + "&nvc_PassWord=" + mVerifiCodeEt.getText().toString().trim();
                HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                        LoginActivity.this, User.class, "");
                HttpHandleUtil.executeCommonPost(LoginActivity.this, httpRequestBean);
            }
        } else {
            CUtils.showToast("网络不可用，请修复网络！");
        }
    }

    /**
     * 保存用户信息
     *
     * @param user
     */
    private void saveUserInfo(User user) {
        if (user != null) {
            DataWareHourse.getInstance().setmUser(user);
            User.DataBean dataBean = user.getData().get(0);
            ((GlobalApplication) getApplication()).setLogin(true);
            CacheUtils.setBooleanToCache(this, CacheUtils.islogin, true);
            CacheUtils.saveObjectToTextFile(RequestCode.TOLOGIN_ACTIVITY_REQUESTCODE, user);
            CacheUtils.setStringToCache(this, CacheUtils.phoneNum, dataBean.getNvc_MobilePhone());
            CacheUtils.setStringToCache(this, CacheUtils.verification, mVerifiCodeEt.getText().toString().trim());
        }
    }

    /**
     * @param finishActivity 这个是用来给启动页面进入后登录用到的，如果是启动页面登录后，需要关闭，其他页面用不到,随便逛逛则不能关闭登录页面
     */
    private void setLoginFinishResult(boolean finishActivity) {
        if (isCanBack) {//如果不是从启动页面进来的，其他页面是可以返回结果并销毁页面
            setResult(LOGIN_OK);
            finish();
            MyActivityManager myActivityManager = ((GlobalApplication) getApplication()).getActivityManager();
            myActivityManager.finishActivity("LoginActivity");
        } else {//启动页面登录后直接去主页面即可
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            if (finishActivity) {
                finish();
            }
        }
    }

    private void startRegActivity() {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivityForResult(intent, GOREGIST);
    }

    //忘记密码（重置密码）
    private void toRestPsdActivity() {
        Intent intent = new Intent(this, RegisterActivity.class);
        intent.putExtra("RESETPASSWORD", "resetPassword");
        startActivity(intent);
        Tools.dismissWaitDialog();
    }

    @Override
    protected void onPause() {
        Tools.dismissWaitDialog();
        super.onPause();
    }

    /**
     * 再按一次退出
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (!isCanBack) {
                if (!mExitFlag) {
                    Toast.makeText(this, R.string.dblclick_to_exit, Toast.LENGTH_SHORT).show();
                    mExitFlag = true;
                    mHandler.sendEmptyMessageDelayed(WHAT_EXIT_FLAG, 2000);
                    return true;
                } else if (mExitFlag) {
                    ((GlobalApplication) getApplication()).getActivityManager().exitApp();
                }
            }
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        Tools.dismissWaitDialog();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        mHandler = null;
        mLogBtn = null;
        mToRegView = null;
        mToMainActivity = null;
        mPhoneNumEt = null;// 手机号
        mVerifiCodeEt = null;// 验证码
        mGetVerifiCodeTv = null;// 获取验证码
        mResetPsdTv = null;
        mBackLay = null;
        phoneNumber = null;
        super.onDestroy();
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof CommonResultBean) {
        } else if (data instanceof User) {
            userBean = (User) data;
            switch (userBean.getResCode()) {
                case "0000":
                    ((GlobalApplication) getApplication()).setLogin(true);
                    saveUserInfo(userBean);
                    setLoginFinishResult(true);
                    break;
                default:
                    if ("登陆失败,请检查账号密码是否正确".equals(userBean.getMsg()) && !isPWD && "0003".equals(userBean.getResCode())) {
                        CUtils.showToast("验证码错误！");
                    } else {
                        CUtils.showToast(userBean.getMsg());
                    }
                    break;
            }
        } else if (data instanceof VerifiCodeLoginBean) {
            VerifiCodeLoginBean response = (VerifiCodeLoginBean) data;
            if (response != null && response.getResCode() != null) {
                if (response.getResCode().equals("0000")) {
                    isSended = true;
                    CUtils.showToast("验证码发送成功，请注意查收");
                } else {
                    CUtils.showToast(response.getMsg());
                }
            }
        }
    }

    @Override
    public void onError(ErrorBean errorBean) {
        if (errorBean != null && errorBean.getRequestCode().equals(Url.VERFICATIONCODE)) {
            mHandler.sendEmptyMessage(GETVERIFIFAIL);
        }
        CUtils.showToast("请求数据错误，请稍后再试！");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SCANNIN_GREQUEST_CODE:
                if (resultCode == RESULT_OK) {
                    Bundle bundle = data.getExtras();
                    //显示扫描到的内容
                    String content = bundle.getString("result");
                    if (isRightUrl(content)) {
                        startBrowser(content);
                    } else {
                        CUtils.showToast("没有该推荐商家");
                        return;
                    }
                }
                break;
            case GOREGIST:
                if (resultCode == RESULT_OK) {
                    String resetNum = CacheUtils.getStringFromCache(LoginActivity.this, CacheUtils.phoneNum);
                    mPhoneNumEt.setText(resetNum);
                    setLoginFinishResult(true);
                }
                break;
            case 600:
                setLoginFinishResult(true);
                break;
        }
    }

    private void startBrowser(String uri) {
        Uri ulf = Uri.parse(uri); //浏览器
        final Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(ulf);

        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                startActivity(intent); //执行
            }
        };
        timer.schedule(task, 1); //10秒后
    }

    private boolean isRightUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return false;
        }
        try {
            WebAddress webAddress = new WebAddress(url);
            URL newurl = new URL(url);
        } catch (Exception e) {

            return false;
        }

        if (url.contains("bun")) {
            return true;
        }
        return false;
    }
}
